<template></template>

<script>
import { deepClone } from "@/utils";

const form = {};
export default {
  name: "BaseIndex",
  data() {
    return {
      buttonLoading: false,
      key: "id",
      moduleName: "",
      isEdit: false, // 是否编辑状态
      loading: true, // 遮罩层
      title: "", // 弹出层标题
      open: false, // 弹出框是否打开
      canChangePass: false,
      showSearch: true, // 显示查询条件
      ids: [], // 选中数组
      total: 0, // 总条数
      list: [], // 页面列表数据
      form: {}, // 弹框表单数据
      queryParams: {}, // 查询请求参数
      rules: [], // 表单校验规则
    };
  },

  computed: {
    showBatch: function () {
      return !!this.ids.length;
    },
  },
  methods: {
    changePassword2(flag) {
      this.canChangePass = flag;
    },
    // ---- 需要复写的方法

    /**
     * 获取列表数据方法
     * @returns {Promise<unknown>}
     */
    getList() {
      return new Promise((resolve, reject) => {
        reject("请复写获取列表数据的方法");
      });
    },
    /**
     * 获取详情数据方法
     * @returns {Promise<unknown>}
     */
    getInfo(id) {
      return new Promise((resolve, reject) => {
        reject("请复写获取详情数据的方法");
      });
    },
    update(form) {
      return new Promise((resolve, reject) => {
        reject("请复写更新数据的方法");
      });
    },
    add(form) {
      return new Promise((resolve, reject) => {
        reject("请复写新增数据的方法");
      });
    },
    del(id) {
      return new Promise((resolve, reject) => {
        reject("请复写删除数据的方法");
      });
    },

    /**
     * 提交之前的数据操作
     */
    beforeSubmit() {},

    // ---- 通用逻辑

    /**
     * 获取列表统一入口
     */
    handleList() {
      this.loading = true;
      this.getList()
        .then(({ list, total }) => {
          this.list = list;
          this.total = total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.handleList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.isEdit = false;
      this.reset();
      // console.log('form: ',this.form);
      this.open = true;
      this.title = "新增数据库";
    },
    /** 编辑按钮操作 */
    handleEdit(id) {
      this.isEdit = true;
      this.title = "编辑数据库";
      this.reset();

      this.handleInfo(id).finally(() => {
        this.open = true;
      });
    },
    /**
     * 处理详情信息
     */
    handleInfo(id) {
      return new Promise((resolve, reject) => {
        this.getInfo(id)
          .then((info) => {
            Reflect.ownKeys(this.form).forEach((k) => (this.form[k] = info[k]));
            resolve();
            // this.form = info
          })
          .catch((err) => {
            reject();
          });
      });
    },

    /** 删除按钮操作 */
    handleDelete(id) {
      if (!id) id = this.ids.join(",");
      this.$GlobalDeleteConfirm(
        "删除数据库将同时删除对应的扫描结果信息，确认删除？"
      )
        .then(() => {
          this.del(id).then((res) => {
            if (res && (res.code === 200 || res.rescode === 200)) {
              this.$GlobalSuccessMessage("删除成功");
              // this.queryParams.pageNum=1
              if (
                this.queryParams.pageNum > 1 &&
                id.toString().split(",").length === this.list.length
              ) {
                this.queryParams.pageNum -= 1;
              }
              this.handleList();
            } else {
              this.$GlobalErrorConfirm("删除失败", res.msg);
            }
          });
        })
        .catch(() => {});
    },

    /** 提交按钮 */
    submitForm() {
      // let params = deepClone(this.form)
      // console.log(params[this.key])
      // if (params[this.key] != null) {
      //   console.log(123)
      // }else{
      //   console.log(456)
      // }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          // this.open = false
          let params = deepClone(this.form);
          this.beforeSubmit(params);
          if (params[this.key]) {
            this.update(params)
              .then((res) => {
                if (res && (res.code === 200 || res.rescode === 200)) {
                  this.$GlobalSuccessMessage("编辑成功");
                  this.handleList();
                  this.canChangePass = false;
                  this.open = false;
                } else {
                  this.$GlobalErrorConfirm("编辑失败", res.msg);
                }
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            this.add(params)
              .then((res) => {
                if (res && (res.code === 200 || res.rescode === 200)) {
                  this.$GlobalSuccessMessage("新增成功");
                  this.open = false;
                  this.handleList();
                  this.canChangePass = false;
                } else {
                  this.$GlobalErrorConfirm("新增失败", res.msg);
                }
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },

    /**
     * 弹框取消
     */
    cancel() {
      this.reset();
      this.open = false;
      this.canChangePass = false;
    },
    // 表单重置
    reset() {
      Reflect.ownKeys(this.form).forEach((k) => (this.form[k] = form[k]));
      this.resetForm("form");
    },

    resetForm(refName) {
      if (this.$refs[refName]) {
        this.$refs[refName].resetFields();
      }
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item[this.key]);
    },
  },
  created() {
    // this.handleList()
  },
  mounted() {},
};
</script>

<style scoped></style>
